<template>
  <div>
    <p></p>
    <el-form ref="form" :model="form" label-width="80px" >
      <el-form-item label="书名" style="text-align: left;width: 520px" prop="name">
        <el-input v-model="form.bookname"></el-input>
      </el-form-item>
      <el-form-item label="作者" style="text-align: left;width: 520px" prop="auth">
        <el-input v-model="form.author"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">修改</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>

  </div>

</template>

<script>
export default {
  data() {
    return {
      form: {
        bookname: '',
        author: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入book名称', trigger: 'blur'}
        ],
        auth: [
          {required: true, message: "输入作者", trigger: "blur"}
        ]
      }
    }

  }, created() {
    let id = this.$route.query.id;
    let _this = this;
    axios.get('http://localhost:8888/books/find/' + id).then(function (response) {
      _this.form = response.data
    })

  },
  methods: {
    onSubmit(formName) {    this.$refs[formName].validate((valid) => {
      if (valid) {
        let _this = this
        axios.put('http://localhost:8888/books/update',this.form).then(function (response) {
          if(response.data){
            _this.$alert(_this.form.bookname+'修改成功！', '修改数据', {
              confirmButtonText: '确定',
              callback: action => {
                //跳转到/table
                _this.$router.push('/list')
              }
            });
          }
        })
      }
    });
    }

  }
}
</script>